<template>
  <el-dialog title="强制下架" :visible.sync="isShow" width="500px"
             @close="$emit('update:isShowDialog', false)" :before-close="beforeClose">
    <el-form label-position="right" ref="info" :rules="rules" :model="info" label-width="110px">
      <el-form-item label="商品名称:">
        <span>{{productInfo.productName}}</span>
      </el-form-item>
      <el-form-item label="供应商名称:">
        <span>{{productInfo.enterpriseName}}</span>
      </el-form-item>
      <el-form-item label="强制下架原因:" prop="explainDesc">
        <el-input type="textarea" v-model="info.explainDesc"
                  :autosize="{ minRows: 3, maxRows: 5}"></el-input>
      </el-form-item>
    </el-form>
    <div class="footer text-center">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit('info')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    name: "updateProductStatus",
    props: {
      isShowDialog: {
        type: Boolean,
        default: false
      },
      pInfo:{
        default:()=>{
          return{
            productName:"",
            enterpriseName:"",
            id:null
          }
        }
      }
    },
    data() {
      return {
        isShow: this.isShowDialog,
        info: {
          explainDesc:""
        },
        productInfo:{...this.pInfo},
        rules: {
          explainDesc: [
            {required: true, message: '请输入强制下架原因', trigger: 'blur'},
            {validator: validate.length150, trigger: 'change'}
          ]
        }
      }
    },
    watch: {
      isShowDialog(val) {
        this.isShow = val
      },
      pInfo(val) {
        this.productInfo = {...val}
      }
    },
    methods: {
      beforeClose(done){
        this.info.explainDesc = ""
        done()
      },
      cancel(){
        this.isShow = false
        this.info.explainDesc = ""
      },
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.updateProductStatus()
          }
          else {
            return false;
          }
        });
      },
      updateProductStatus() {
        ajax.post(apis.productAudit.updateProductStatus, {id:this.productInfo.id,explainDesc:this.info.explainDesc})
          .then(res => {
            if (res.success === true) {
              this.$message({
                message: res.message,
                type: 'success'
              });
              this.cancel()
              this.$emit("getProductList")
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
            }
          })
      }
    },
    created() {
    }
  }
</script>

<style lang="scss" scoped>
.footer{
  margin-top: 40px;
}
</style>
